import { createRouter, createWebHistory } from 'vue-router'
import store from '@/store'


import Layout from '@/views/home.vue'
import Files from '@/pages/home/files.vue'
import Home from '@/pages/home/homePage.vue'
import User from '@/pages/home/userPage.vue'
import NotFound from '@/pages/error/404.vue'

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login.vue')
  },
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'home',
        component: Home
      },
      {
        path: 'user',
        name: 'user',
        component: User
      },
      {
        path: 'files',
        name: 'files',
        component: Files
      }
    ]
  },
  {
    path: '/test',
    name: 'test page',
    component: () => import('@/views/testPage.vue'),
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: NotFound
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 导航守卫（路由切换过程中使用，对切换行为进行限制）
router.beforeEach((to, from, next) => {
  // console.log("to Path: " + to.path);
  let token = store.state.UserToken
  if (!token && to.path !== '/login') {
    next({ path: '/login' })
  } else if (token && to.path === '/login') {
    next({ path: '/home' })
  } else (
    next()
  )
})

export default router